#include "xmakedoc.h"
#include "qrcutil.h"

#include <QAction>
#include <QListWidgetItem>
#include <QWebEngineProfile>
#include <QWebEngineScriptCollection>
#include <QWebEngineView>
#include <httpclient.h>
#include <qd.h>
#include <response.h>

// static QList<QVariantMap> load(QVariantMap map) {
//     QList<QVariantMap> maps;
//     auto keys = map.keys();
//     if (keys.length() == 3) {
//         maps << map;
//     } else {
//         foreach (auto key, map.keys()) {
//             auto v = map.value(key).toMap();
//             maps << load(v);
//         }
//     }
//     return maps;
// }

XMakeDoc::XMakeDoc()
{
    foreach (auto line, QR("://xmake-doc.txt").split("\n")) {
        auto splits = line.split(", ");
        if (splits.length() != 2) continue;

        auto link = splits.at(0).trimmed();
        auto name = splits.at(1).trimmed();
        add_category(name, link);
    }
}

QString XMakeDoc::name()
{
    return "XMake Doc";
}

QString XMakeDoc::prefix()
{
    return "https://xmake.io/";
}

QList<QListWidgetItem *> XMakeDoc::searching_item(const QString &text)
{
    QList<QListWidgetItem *> list;
    // foreach (auto var, database) {
    //     auto title = var.value("title").toString();
    //     auto slug = var.value("slug").toString();

    //     if (text.isEmpty() || title.contains(text)) {
    //         auto item = new QListWidgetItem;
    //         item->setText(title);
    //         item->setData(990, slug);
    //         list << item;
    //     }
    // }
    if (m_interactive == 0) {
        foreach (auto category, m_categories) {
            if (text.isEmpty() || category.contains(text, Qt::CaseInsensitive)) {
                auto item = newItem();
                item->setText(category);
                list << item;
            }
        }
    }

    if (m_interactive == 1) {
        foreach (auto docMap, m_xmake_docs.value(m_category_link)) {
            auto title = docMap.value("title").toString();
            auto slug = docMap.value("slug").toString();

            auto item = newItem();
            item->setText(QString("[%1]: %2").arg(text).arg(title));
            item->setData(990, slug);
            list << item;
        }
    }

    return list;
}

void XMakeDoc::content_item(const QListWidgetItem *item, QWebEngineView *view)
{
    // foreach (auto var, m_xmake_docs.value(m_category_link)) {
    //     auto title = var.value("title").toString();
    //     auto slug = var.value("slug").toString();
    //     auto body = var.value("body").toString();
    //     if (item->data(990).toString().compare(slug) == 0) {
    //         if (true) {
    //             qd << prefix() + slug;
    //             auto url = prefix() + slug;
    //             view->setUrl(url);
    //             view->page()->setUrl(url);
    //             view->load(url);
    //             // view->load(prefix() + slug);
    //             // view->reload();
    //         } else {
    //             // return "<pre>" +body+ "</pre>";
    //         }
    //     }
    // }
    auto url = prefix() + item->data(990).toString();
    view->load(QUrl(url));

    QWebEngineProfile *profile = view->page()->profile();
    QWebEngineScriptCollection *scripts = profile->scripts();
    // scripts->clear();

    QWebEngineScript self;
    self.setSourceCode(QString("console.log(document.querySelector(\"a[href='%1']\"));document.querySelector(\"a[href='%1']\").click();").arg(item->data(990).toString()));
    scripts->insert(self);
    // scripts->remove(self);


    // auto page = new QWebEnginePage;
    // auto page = view->page();
    // page->setUrl(url);
    // QAction*a = page->action(QWebEnginePage::OpenLinkInThisWindow);
    // a->trigger();
    // qd << url;
    // view->setPage(page);

    // view->load(QUrl(url));
    // m_once_reload = true;

    // QWebEngineHttpRequest request(url);
    // request.setMethod(QWebEngineHttpRequest::Get);
    // view->load(request);
}

bool XMakeDoc::interactive(const QString &text) {
    if (m_interactive == 0) {
        m_category = text;
        m_category_link = m_category_links.value(m_category);
        if (m_xmake_docs.value(m_category_link).isEmpty()) {
            m_xmake_docs.insert(m_category_link, xmake_docs(m_category_link));
        }
        return m_interactive++ < 1;
    }

    return false;
}

bool XMakeDoc::interactive_running() {
    return m_interactive != 0;
}

void XMakeDoc::interactive_restore() {
    m_interactive = 0;
}

void XMakeDoc::add_category(const QString &category, const QString &link)
{
    m_categories << category;
    m_category_links.insert(category, link);
}

QList<QVariantMap> XMakeDoc::xmake_docs(const QString &link)
{
    auto map = Response(0, QR("://xmake-doc.json")).toMap();
    auto docMap = map.value(link).toMap();

    QList<QVariantMap> list;
    foreach (auto dockey, docMap.keys()) {
        list << docMap.value(dockey).toMap();
    }
    return list;
}

